package com.kerrykidz.sms.util;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.kerrykidz.sms.dao.model.SmsBatchEntity;
import com.kerrykidz.sms.dao.model.SmsContEntity;
import com.kerrykidz.sms.dao.model.SmsOutBoxEntity;
import com.kerrykidz.sms.service.IBatchService;
import com.kerrykidz.sms.service.ISmsSendService;
import com.kerrykidz.sms.service.impl.BatchServiceImpl;
import com.kerrykidz.sms.service.impl.SmsSendServiceImpl;
import com.kerrykidz.system.util.DateUtil;
import com.rdp.framework.exception.RDPException;
import com.rdp.util.config.SpringUtil;

/***
 * 定时发送短信消息
 * @ClassName: SmsTask 
 * @description: 大致描述该类的用途，内容简短易懂 
 * @author: wolf-yansl 
 * @date: 2014年10月8日 下午12:00:57 
 * @version: V1.0
 *
 */
public class SmsTask extends QuartzJobBean{
	private Logger logger = Logger.getLogger(SmsTask.class);  
	

	@Override
	protected void executeInternal(JobExecutionContext context)throws JobExecutionException {
		ISmsSendService smsService = (ISmsSendService) SpringUtil.getBean("smsSendServiceImpl");
		IBatchService batchService = (IBatchService) SpringUtil.getBean("batchServiceImpl");
		
		/**查询条件*/
		Date currentDate = new Date();
		String formatDate = "yyyy-MM-dd HH:mm:ss";
		SmsBatchEntity smsBatche = new SmsBatchEntity();
		smsBatche.setStartDate(DateUtil.formatDate(formatDate, currentDate));
		smsBatche.setEndDate(DateUtil.formatDate(formatDate,DateUtil.addMinute2Date(1, currentDate)));
		smsBatche.setStatus(SmsBatchEntity.STATUS_TJ);
		
		/**处理结果*/
		try {
			List<SmsBatchEntity> resultBatchList = batchService.findSmsBatchEntity(smsBatche);
			if(resultBatchList != null && resultBatchList.size() > 0){
				for(SmsBatchEntity entity : resultBatchList){
					/*设置正在发送中*/
					batchService.updateCloseSmsBatchEntity(entity);
					
					/**查询批次模板内容*/
					SmsContEntity parmCont = new SmsContEntity();
					parmCont.setPkSmsContId(entity.getFkSmscontId());
					List<SmsContEntity> contList = batchService.findSmsContEntity(parmCont);
					if(contList != null && contList.size() == 1){
						parmCont = contList.get(0);
					}
					
					/**查询批次手机号码*/
					SmsOutBoxEntity parmBox = new SmsOutBoxEntity();
					parmBox.setFkBatchId(entity.getPkBatchId());
					List<SmsOutBoxEntity> boxList = batchService.findSmsOutBoxEntity(parmBox);
					StringBuffer sb = new StringBuffer();
					if(boxList != null && boxList.size() > 0 ){
						for(SmsOutBoxEntity box : boxList){
							sb.append(box.getDestPhone());
							sb.append(",");
						}
					}
					String smsMob = sb.toString();
					String smsText = sb.toString();
					
					/**发送短信*/
					if(BatchCondUtil.isBlank(smsMob,smsText)){
						SmsSendVO  smsVO = new SmsSendVO();
						smsVO.setBatchFlag(true);
						smsVO.setSmsBatchId(entity.getPkBatchId());
						smsVO.setSmsMob(smsMob);
						smsVO.setSmsText(smsText);
						
						smsService.sendSMSAndUpdateOutBox(smsVO);
					}
				}
			}
			logger.info("短信定时调用。。。。。" + currentDate + "查询结果："+resultBatchList.size());
		} catch (RDPException e) {
			logger.error("发送短信获取批次失败：Error["+e.getMessage()+"]");
		}
	}


}
